package org.litespring.beans.factory.support;

import org.litespring.beans.factory.config.SingletonBeanRegistry;
import org.litespring.util.Assert;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {

    private final Map<String,Object> singletonObjects = new ConcurrentHashMap<String, Object>(64);

    public void registerSingleton(String beanName, Object singletonObject) {
        Assert.notNull(beanName,"'beanName' must not be null");
        Object object = this.singletonObjects.get(beanName);
        if(object != null)
        {
            throw new IllegalStateException("Could not register object [" + singletonObject + "] under bean name '" + beanName + "' :" +
                    "there is already object [" + object + " exists");
        }
        this.singletonObjects.put(beanName,singletonObject);
    }

    public Object getSingleton(String beanName) {
        return singletonObjects.get(beanName);
    }
}
